import {
  CHANGE_INPUT_VALUE,
  ADD_TODO_ITEM,
  DELETE_TODO_ITEM,
  INIT_LIST_ACTION,
} from "./actiontypes";

//图书馆记事本原理
const defaultState = {
  inputValue: "",
  list: [],
};
//空对象
// reducer 可以接收state 但是绝对不能修改state
export default (state = defaultState, action) => {
  if (action.type === CHANGE_INPUT_VALUE) {
    const newState = JSON.parse(JSON.stringify(state)); //深拷贝
    newState.inputValue = action.value;
    return newState;
  }

  if (action.type === INIT_LIST_ACTION) {
    const newState = JSON.parse(JSON.stringify(state)); //深拷贝
    newState.list = action.data;
    return newState;
  }

  if (action.type === ADD_TODO_ITEM) {
    const newState = JSON.parse(JSON.stringify(state)); //深拷贝
    newState.list.push(newState.inputValue);
    newState.inputValue = "";
    return newState;
  }

  if (action.type === DELETE_TODO_ITEM) {
    const newState = JSON.parse(JSON.stringify(state)); //深拷贝
    newState.list.splice(newState.index, 1);
    return newState;
  }
  return state;
};
//state 整个store存储数据
